﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace MusicDatabase
{/*
     * Auteur:  Joris Molnár en Niels van Leusden
     * Datum:   31-1-2013
     * Omschrijving: Door middel van deze klasse worden alle methodes aangeroepen bij bepaalde acties.
     *               En zo worden alle andere klassen dan gebruikt waarvoor ze bedoelt zijn.
     *
     */
    public partial class Main : Form
    {
        private DataTable dataTable = new DataTable();
        private FillMain fillMain = new FillMain();

        private string username;

        public Main()
        {
            InitializeComponent();

            this.MinimumSize = new System.Drawing.Size(785, 300);
            Login();
        }

        /// <summary>
        /// Show the Login Form and hide the main Form
        /// </summary>
        private void Login()
        {
            Login loginForm = new Login();
            this.Hide();
            loginForm.ShowDialog();
            if (loginForm.DialogResult == DialogResult.OK)
            {
                username = loginForm.Nick;
                this.Show();
                slbUser.Text = "User: " + username;
            }
        }

        private void Main_Load(object sender, EventArgs e)
        {
            fillMain.FillMainDgv(dgvMain);            
        }

        private void dgvMain_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (!(e.RowIndex >= 0)) return; // Ignore Double Clicking outside of a cell
            int albumID = Convert.ToInt32(dgvMain.Rows[e.RowIndex].Cells[0].FormattedValue);
            
            AlbumInfo albuminfo = new AlbumInfo(albumID);
            albuminfo.Show();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            DialogResult dialogresult = MessageBox.Show("Are you sure you want to delete the selected albums?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            
            if (dialogresult == DialogResult.Yes)
            {
                List<int> albumID = new List<int>(); // Get the selected rows from the cells
                foreach (DataGridViewCell cell in dgvMain.SelectedCells)
                {
                    albumID.Add(Convert.ToInt32(dgvMain.Rows[cell.RowIndex].Cells[0].FormattedValue));
                }
                albumID = new HashSet<int>(albumID).ToList<int>();

                Album album = new Album();
                album.DeleteAlbums(albumID);

                tbSearchArtist.Text = "";
                tbSearchName.Text = "";
                tbSearchSong.Text = "";
                dgvMain.Columns[0].Visible = true;
                dgvMain.Columns.Clear();
                fillMain.FillMainDgv(dgvMain);
            }
            else
            {
                return;
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            AddAlbum addAlbum = new AddAlbum();
            addAlbum.ShowDialog();
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            tbSearchArtist.Text = "";
            tbSearchName.Text = "";
            tbSearchSong.Text = "";
            dgvMain.Columns[0].Visible = true;
            dgvMain.Columns.Clear();
            fillMain.FillMainDgv(dgvMain);
            
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            fillMain.SearchDgv(dgvMain, tbSearchName.Text, tbSearchArtist.Text, tbSearchSong.Text);
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            List<int> albumList = new List<int>(); // Get the selected rows from the cells
            foreach (DataGridViewCell cell in dgvMain.SelectedCells)
            {
                albumList.Add(Convert.ToInt32(dgvMain.Rows[cell.RowIndex].Cells[0].FormattedValue));
            }
            albumList = new HashSet<int>(albumList).ToList<int>();

            if (albumList.Count() != 1)
            {
                MessageBox.Show("Please select only one album to edit.");
                return;
            }
            int albumID = albumList[0];
            AddAlbum editAlbum = new AddAlbum(albumID);
            editAlbum.ShowDialog();
        }
    }
}










